package com.fanghaoran.notes.dao.impl;

import com.fanghaoran.notes.dao.CartDao;
import com.fanghaoran.notes.entity.Cart;
import com.fanghaoran.notes.util.BaseDaoUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/04/22/14:05
 * @Description: 实现类
 */
public class CartDaoImpl implements CartDao {
    @Override
    public List<Cart> selectCartByUidAndPid(int uId, int pId) throws Exception {
        String sql="select c_id,cart.u_id,cart.p_id,product.p_name,product.price,cart.num,cart.count from cart " +
                "join product on cart.p_id=product.p_id where cart.u_id=? and cart.p_id=?;";
        return BaseDaoUtil.executeQuery(Cart.class,sql,uId,pId);
    }

    @Override
    public Integer insertCart(Cart cart) throws Exception {
        String sql="insert into cart(u_id,p_id,num,count) values(?,?,?,?);";
        return BaseDaoUtil.executeUpdate(sql,cart.getUId(),cart.getPId(),cart.getNum(),cart.getCount());
    }

    @Override
    public Integer updateCart(Cart cart) throws SQLException {
        String sql="update cart set num=?,count=? where u_id=? and p_id=?;";
        return BaseDaoUtil.executeUpdate(sql,cart.getNum(),cart.getCount(),cart.getUId(),cart.getPId());
    }

    @Override
    public List<Cart> selectCartByUid(int uId) throws Exception {
        String sql="select c_id,cart.u_id,cart.p_id,product.p_name,product.price,cart.num,cart.count from cart " +
                "join product on cart.p_id=product.p_id where cart.u_id=?;";
        return BaseDaoUtil.executeQuery(Cart.class,sql,uId);
    }

    @Override
    public Integer deleteCartByPid(int pId) throws SQLException {
        String sql="delete from cart where p_id=? ;";
        return BaseDaoUtil.executeUpdate(sql,pId);
    }

    @Override
    public Integer deleteCartByUid(int uId) throws SQLException {
        String sql="delete from cart where u_id=?;";
        return BaseDaoUtil.executeUpdate(sql,uId);
    }
}
